BlogsContact

Computing

2023-03-02 React Native WSL2 Android Emulation

I have spent most of my development career using Linux for software development. I find it has come to feel like a favourite old jumper, very confortable and feels like home.

Having said that now I have semi retired my main pc is a windows laptop running Windows 11, I am quite happy with Windows, I have been using it forever and I actually went to a Launch party of 3.11 in Tucson with Bill Gates as speaker. It works well for my daily needs, email, writing blog posts, editing photos, zoom etc. but for development without doubt Linux is my choice, others will feel differently and its somewhat horses for courses.

Now with Windows Subsystem for Linux WSL2, which gives me a full linux environment in a closely integrated container within windows. It works really well for me, in fact I am typing this blog post on Ghostwriter running on Ubuntu 20 in WSL2.

I have already developed some great applications using this settup including an Email publishing system in Adobe Campaign, a React Web application for data recording and a PWA for logging locatations.

Now I have retired from fulltime employment, I want to use some of my spare time producing some Mobile Applications that I have previously created for myself and others but never published.

The current App I am working on is to be completed in React-Native, its very easy to install this in Linux and works really well, until I want to run the App in an Android emulator, it does run in WSL2 but not reliably, I really needed to load the App in an emulator in Windows.

So my aim is to create and build the application in WSL2, using nodejs, Visual Studio etc. and then launch the App on an emulator running in Windows.

To get me started I followed these instructions Building a react native app in WSL2

These worked very well. I did look at some others but this seems the best to get you started and are easy to understand. There are also many comments that will help. For example I see there is a suggestion to use a USB connected device directly from WSL which I will try at some point.

I did make the following notes:

  1. I use McAfee for my firewall and I had to enable TCP port 5037
  2. I used Android studio on windows as an easy way to create an emulated device
  3. I used the socat utility as this was more reliable than the environment variable although I did find that socat did not always terminate cleanly when I have finished so I usually shutdown the server at the end of a session.
  4. I had no port forwarding issues so i did not need to use netsh
  5. I am only using Virtual Devices so have not tried USB connected but will do at some point and will do another post if I discover anything interesting.
  6. I did make some changes to package.json in my react app (see below)

Here are some of the scripts that I run to start me up in this particular order:

In Windows

Start an emulator

[path to emulator]\emulator.exe -avd pixel4a

Start the ADB server

adb kill-server
adb -a nodaemon server start

In Linux

Start socat

socat -d -d TCP-LISTEN:5037,reuseaddr,fork TCP:$(cat /etc/resolv.conf | awk '/nameserver/ {print $2}'):5037

At that point you should be able run the command adb devices in windows and wsl2 and you should see the enulator listed in both.

Changes to package.json

In my package.json I made the following change in the scripts section to bind to an ipv4 address to enable port forwarding to windows. I did not specify the device on my android script entry as I am using the virtual emulator.

    "start": "react-native start --host 127.0.0.1",

I am very happy with the way this works

© Jeremy Smith